/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Frame.java
 *
 * Created on 07.12.2010, 13:38:00
 */

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 
 * @author JAD!
 */
public class Frame extends javax.swing.JFrame {

	int server_port = 800;
	Servr serv = null;
	Client cl = null;

	/** Creates new form Frame */
	public Frame() {
		initComponents();

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jToggleButton1 = new javax.swing.JToggleButton();
		serverPort = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jSeparator1 = new javax.swing.JSeparator();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jTextField2 = new javax.swing.JTextField();
		jButton2 = new javax.swing.JButton();
		jSeparator2 = new javax.swing.JSeparator();
		serverAriaGet = new javax.swing.JScrollPane();
		jTextArea3 = new javax.swing.JTextArea();
		jScrollPane3 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		jLabel6 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jToggleButton1.setText("Запустить сервер");
		jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jToggleButton1ActionPerformed(evt);
			}
		});

		serverPort.setText("800");

		jLabel1.setText("Port:");

		jLabel2.setText("Server");

		jLabel3.setText("Client");

		jLabel4.setText("IP");

		jLabel5.setText("Port");

		jTextField1.setText("127.0.0.1");

		jTextField2.setText("800");

		jButton2.setText("Connect");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jTextArea3.setColumns(20);
		jTextArea3.setRows(5);
		serverAriaGet.setViewportView(jTextArea3);

		jTextArea1.setColumns(20);
		jTextArea1.setRows(5);
		jScrollPane3.setViewportView(jTextArea1);

		jLabel6.setText("Подключенных клиентов:");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout.createSequentialGroup().addGap(241, 241,
										241).addComponent(jLabel2)
										.addContainerGap(227, Short.MAX_VALUE))
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jSeparator1,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												480, Short.MAX_VALUE)
										.addContainerGap())
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																false)
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel5)
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				jTextField2))
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel4)
																		.addGap(
																				30,
																				30,
																				30)
																		.addComponent(
																				jTextField1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				169,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jLabel3))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				47,
																				47,
																				47)
																		.addComponent(
																				jButton2)))
										.addContainerGap(161, Short.MAX_VALUE))
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jSeparator2,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												480, Short.MAX_VALUE)
										.addContainerGap())
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel1)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				serverPort,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				298,
																				Short.MAX_VALUE))
														.addComponent(
																serverAriaGet,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																326,
																Short.MAX_VALUE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jToggleButton1)
														.addComponent(jLabel6))
										.addGap(24, 24, 24))
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jScrollPane3,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												360,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(130, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(jLabel2)
										.addGap(11, 11, 11)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jToggleButton1)
														.addComponent(jLabel1)
														.addComponent(
																serverPort,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				15,
																				15,
																				15)
																		.addComponent(
																				jLabel6,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				24,
																				Short.MAX_VALUE)
																		.addGap(
																				40,
																				40,
																				40))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				serverAriaGet,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				55,
																				Short.MAX_VALUE)
																		.addGap(
																				18,
																				18,
																				18)))
										.addComponent(
												jSeparator1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												10,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(7, 7, 7)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				7,
																				7,
																				7)
																		.addComponent(
																				jLabel3)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jButton2))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				18,
																				18,
																				18)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jLabel4)
																						.addComponent(
																								jTextField1,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jLabel5)
																						.addComponent(
																								jTextField2,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE))))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jScrollPane3,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												55,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addComponent(
												jSeparator2,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(76, 76, 76)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jToggleButton1ActionPerformed
		try {
			server_port = Integer.parseInt(this.serverPort.getText());
		} catch (Exception e) {

			this.jToggleButton1.setText("Запустить сервер");
			this.jToggleButton1.setSelected(false);
			this.serverPort
					.setText("Введите номер порта, например 800");
			return;
		}
		// System.out.println(serv.getState());
		if (serv == null) {
			this.jToggleButton1.setText("Остановить сервер");
			this.jToggleButton1.setSelected(true);
			serv = new Servr(server_port);
			serv.start();
			return;
		}

		if (serv.isSuspended()) {

			this.jToggleButton1.setText("Остановить сервер");
			this.jToggleButton1.setSelected(true);
			serv.setSuspended(false);
			System.out.println(serv.getState() + " Server runed, suspend="
					+ serv.isSuspended());
			return;
		}

		else {
			this.jToggleButton1.setText("Запустить сервер");
			this.jToggleButton1.setSelected(false);

			serv.setSuspended(true);
			System.out.println(serv.getState() + " Server suspended, suspend="
					+ serv.isSuspended());

			return;
		}

		// TODO add your handling code here:
	}// GEN-LAST:event_jToggleButton1ActionPerformed

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton2ActionPerformed
		Client cli = new Client(this.jTextField1.getText(), Integer
				.parseInt(this.jTextField2.getText()));
		new Thread(cli).start();
		// TODO add your handling code here:
	}// GEN-LAST:event_jButton2ActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Frame().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButton2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JScrollPane jScrollPane3;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JSeparator jSeparator2;
	private javax.swing.JTextArea jTextArea1;
	private javax.swing.JTextArea jTextArea3;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	private javax.swing.JToggleButton jToggleButton1;
	private javax.swing.JScrollPane serverAriaGet;
	private javax.swing.JTextField serverPort;
	// End of variables declaration//GEN-END:variables

}

class Servr extends Thread {

	int server_port;

	public Servr(int port) {
		server_port = port;
	}

	private Server servr = null;
	private boolean suspended = false;

	@Override
	public void run() {

		servr = new Server(server_port);
		servr.socketStarter();
	}

	// @Deprecated
	public boolean isSuspended() {
		return suspended;
	}

	public void setSuspended(boolean r) {
		if (suspended == false && r == true) {
			suspended = true;
			servr.suspend_Serv();
			this.suspend();
		}
		if (suspended == true && r == false) {
			suspended = false;

			this.resume();
			servr.resume();
		}
	}
}